/** @format */

import { CodeType, DEFAULT_CODE_TYPES } from './codetypes'

interface IErrorResult<T = any> {
  success: false
  code: string
  message?: string
  data?: T
}
interface ISuccessResult<T = any> {
  code: string
  success: true
  message?: string
  data?: T
}
export class ResultsModel {
  /**
   *  不填errorType,默认为系统错误
   * @param errorType
   * @param message
   * @param data
   */
  error<T>(errorType: CodeType, message?: string, data?: T): IErrorResult<T> {
    if (!errorType) {
      errorType = DEFAULT_CODE_TYPES.SYSTEM_ERROR
    }
    return {
      success: false,
      code: errorType.code,
      message: message || errorType.defaultMsg,
      data
    }
  }

  success<T>(data: T): ISuccessResult<T> {
    return {
      success: true,
      code: DEFAULT_CODE_TYPES.SUCCESS.code,
      data,
      message: DEFAULT_CODE_TYPES.SUCCESS.defaultMsg
    }
  }
}
